package com.shanyue.redwine.http;

import android.app.Activity;

import com.shanyue.redwine.http.rx.RxBus;
import com.shanyue.redwine.utils.ApiException;
import com.shanyue.redwine.utils.SweetAlertDialogFactory;

import java.io.IOException;
import java.net.ConnectException;
import java.net.SocketTimeoutException;

import cn.pedant.SweetAlert.SweetAlertDialog;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 项目：MeiYueHongJiu  包名：com.shanyue.redwine.http
 * <p>
 * huangxiaoyu
 * <p>
 * 2018/5/17
 * <p>
 * desc  实用与统一格式返回的数据
 */
public abstract class ApiCallBack<T> implements Callback<ApiResponse<T>> {
    Activity mContext;
    SweetAlertDialog mLoadind;
    boolean finishActivity = false;

    public ApiCallBack(Activity context) {
        mContext = context;
    }

    public ApiCallBack(Activity context, SweetAlertDialog loadind) {
        mContext = context;
        mLoadind = loadind;
    }

    public ApiCallBack setFinishActivity() {
        this.finishActivity = true;
        return this;
    }

    /**
     * 成功回调
     *
     * @param response
     */
    protected abstract void onResponse(ApiResponse<T> response);

    /**
     * 失败回调 已弹出默认提示 默认提示参考:{@see onFailure()}
     *
     * @param t
     */
    public void onFailure(Throwable t) {
    }


    @Override
    public void onResponse(Call<ApiResponse<T>> call, Response<ApiResponse<T>> response) {
        if (mContext.isDestroyed()) {
            return;
        }
        if (mLoadind != null) {
            mLoadind.dismiss();
        }
        try {
            if (response.isSuccessful()) {
                if (response.body().success()) {
                    onResponse(response.body());
                } else {
                    onFailure(call, new ApiException(response.body().type, response.body().msg));
                }
            } else {
                if (response.code() == 404) {
                    onFailure(call, new ApiException(response.code(), "服务器无响应"));
                } else if (response.code() == 500) {
                    onFailure(call, new ApiException(response.code(), "服务器内部错误"));
                } else {
                    onFailure(call, new ApiException(response.code(), response.message()));
                }
            }
        } catch (Exception ex) {
            onFailure(call, new ApiException(response.code(), ex.getMessage()));
        }
    }

    @Override
    public void onFailure(Call<ApiResponse<T>> call, Throwable t) {
        if (mContext.isDestroyed()) {
            return;
        }

        if (mLoadind != null) {
            mLoadind.dismiss();
        }
        onFailure(t);
        if (call.isCanceled()) {
            return;
        }
        String hintText = "请求异常";
        if (t instanceof SocketTimeoutException) {
            hintText = "请求超时";
        } else if (t instanceof ApiException) {
            hintText = t.getMessage();
        } else if (t instanceof IOException) {
            hintText = "数据请求异常";
        } else if (t instanceof ConnectException) {
            hintText = "服务器连接失败";
        }
        if (!finishActivity) {
            SweetAlertDialogFactory.build(mContext, SweetAlertDialog.ERROR_TYPE, false).setContentText(hintText).show();
        } else {
            SweetAlertDialogFactory.build(mContext, SweetAlertDialog.ERROR_TYPE, false).setContentText(hintText)
                    .setConfirmClickListener(dialog -> mContext.onBackPressed()).show();
        }

    }

}
